package eerbaar.shared.message;

import eerbaar.shared.service.ServiceFactory;

public class MsgHelper {

    public static <M extends Message, R extends IMessageReader> void registerReader(
            Class<M> type, R handler) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);

        IMessageReaderList list = service.getReaders(type);
        list.addReader(handler);
    }

    public static <R extends IMessageReader> void registerReader(R reader) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);

        IMessageReaderList list = service.getReadersForAllMsgs();
        list.addReader(reader);
    }

    public static void registerReader(String type, IMessageReader handler) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);

        IMessageReaderList list = service.getReaders(type);
        list.addReader(handler);
    }

    public static <M extends Message> void sendMsg(Class<M> type, M msg) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);
        assert (service != null);

        if (service != null) {
            IMessageReaderList list = service.getReaders(type);
            list.sendMessage(msg);
        }
    }

    public static void sendMsg(String type, Message msg) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);
        assert (service != null);

        if (service != null) {
            IMessageReaderList list = service.getReaders(type);
            list.sendMessage(msg);
        }
    }

    public static void sendToDefaultReaders(Message msg) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);
        assert (service != null);

        if (service != null) {
            IMessageReaderList list = service.getReaders("default");
            list.sendMessage(msg);
        }
    }

    public static <M extends Message> void setDebug(Class<M> type, boolean debug) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);

        IMessageReaderList list = service.getReaders(type);
        list.setDebug(debug);
    }

    public static <M extends Message, R extends IMessageReader> void unregisterReader(
            Class<M> type, R reader) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);

        IMessageReaderList list = service.getReaders(type);
        list.removeReader(reader);
    }

    public static <R extends IMessageReader> void unregisterReader(R reader) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);

        IMessageReaderList list = service.getReadersForAllMsgs();
        list.removeReader(reader);
    }

    public static void unregisterReader(String type, IMessageReader reader) {

        IMessageService service = ServiceFactory
                .getService(IMessageService.class);

        IMessageReaderList list = service.getReaders(type);
        list.removeReader(reader);
    }

}
